package dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dao.EmpoyeeDao;
import po.emp.Department;
import po.emp.Empoyee;

public class EmpoyeeDaoImpl implements EmpoyeeDao{

	private List<Empoyee> empoyees;
	
	public EmpoyeeDaoImpl() {
		empoyees = new ArrayList<Empoyee>();
	}
	
	@Override
	public boolean insert(Empoyee t) {
		if(empoyees.contains(t)) {
			return false;
		}
		return empoyees.add(t);
	}

	@Override
	public boolean update(Empoyee t) {
		int index = empoyees.indexOf(t);
		if(index == -1) {
			return false;
		}else {
			empoyees.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		Iterator<Empoyee> it = empoyees.iterator();
		while(it.hasNext()) {
			Empoyee e = it.next();
			if(e.getEmpId() == id) {
				it.remove();
			}
		}
		return false;
	}

	@Override
	public Empoyee selectById(int id) {
		for (Empoyee e : empoyees) {
			if(e.getEmpId() == id) {
				return e;
			}
		}
		return null;
	}

	@Override
	public List<Empoyee> selectAll() {
		List<Empoyee> copy = new ArrayList<>(empoyees);
		return copy;
	}

	@Override
	public List<Empoyee> selectByDepart(Department depart) {
		List<Empoyee> dep = new ArrayList<>();
		for (Empoyee e : empoyees) {
			if(e.getDepart().equals(depart)) {
				dep.add(e);
			}
		}
		return dep;
	}

}
